$(function() {
  FastClick.attach(document.body);
});
$().bind('touchmove', first(), false);

function first() {
	$("#p6").height($("#p1").height());

	var pageIndex = 1,
		pageTotal = $('.page').length,
		towards = {
			up: 1,
			right: 2,
			down: 3,
			left: 4
		},
		isAnimating = false;

	//禁用手机默认的触屏滚动行为

	$(document).swipeUp(function() {
		if(isAnimating) return;
		if(pageIndex < pageTotal - 2) {
			pageIndex += 1;
			pageMove(towards.up);
		};
		if(pageIndex == pageTotal - 2) {
			$("body").css("overflow", "scroll");
		}
	})

	$(document).swipeDown(function() {
		if(isAnimating) return;
		if(pageIndex > 1) {
			pageIndex -= 1;
			pageMove(towards.down);
		}
	})

	function pageMove(tw) {
		var lastPage;
		if(tw == '1') {
			if(pageIndex == 1) {
				lastPage = ".page-" + pageTotal;
			} else {
				lastPage = ".page-" + (pageIndex - 1);
			}

		} else if(tw == '3') {
			if(pageIndex == pageTotal) {
				lastPage = ".page-1";
			} else {
				lastPage = ".page-" + (pageIndex + 1);
			}

		}

		var nowPage = ".page-" + pageIndex;

		switch(tw) {
			case towards.up:
				outClass = 'pt-page-moveToTop';
				inClass = 'pt-page-moveFromBottom';
				break;
			case towards.down:
				outClass = 'pt-page-moveToBottom';
				inClass = 'pt-page-moveFromTop';
				break;
		}
		isAnimating = true;
		$(nowPage).removeClass("hide");

		$(lastPage).addClass(outClass);
		$(nowPage).addClass(inClass);

		setTimeout(function() {
			$(lastPage).removeClass('page-current');
			$(lastPage).removeClass(outClass);
			$(lastPage).addClass("hide");
			$(lastPage).find("img").addClass("hide");

			$(nowPage).addClass('page-current');
			$(nowPage).removeClass(inClass);
			$(nowPage).find("img").removeClass("hide");

			isAnimating = false;
		}, 600);
	}

	var scrollFunc = function(e) {
			var direct = 0;
			e = e || window.event;
			if(e.wheelDelta) { //判断浏览器IE，谷歌滑轮事件             
				if(e.wheelDelta > 0) { //当滑轮向上滚动时
					if(isAnimating) return;
					if(pageIndex > 1) {
						pageIndex -= 1;
						pageMove(towards.down);
					}
				}
				if(e.wheelDelta < 0) { //当滑轮向下滚动时
					if(isAnimating) return;
					if(pageIndex < pageTotal - 2) {
						pageIndex += 1;
						pageMove(towards.up);
					};
				}
			} else if(e.detail) { //Firefox滑轮事件
				if(e.detail > 0) { //当滑轮向上滚动时
					if(isAnimating) return;
					if(pageIndex > 1) {
						pageIndex -= 1;
						pageMove(towards.down);
					}
				}
				if(e.detail < 0) { //当滑轮向下滚动时
					if(isAnimating) return;
					if(pageIndex < pageTotal - 2) {
						pageIndex += 1;
						pageMove(towards.up);
					};
				}
			}
		}
		//给页面绑定滑轮滚动事件
	if(document.addEventListener) {
		document.addEventListener('DOMMouseScroll', scrollFunc, false);
	}
	//滚动滑轮触发scrollFunc方法
	window.onmousewheel = document.onmousewheel = scrollFunc;

}

$("#Email").bind("blur", emailCheck, false);
$("#Tel").bind("blur", telCheck, false);
$("#submit").bind("click", submitCheck, false);
var flag = 0;

function submitCheck() {
	var data1 = {

			name: $("#Name").val(),
			tel: $("#Tel").val(),
			email: $("#Email").val(),
			grade: $("#Class").val(),
			apply: $("#Apply").val(),

		} //数据获取
	console.log(JSON.stringify(data1));

	if(telCheck()) {
		if(emailCheck()) {
			if(isEmpty()) {

				$.ajax({
					type: 'POST',
					url: 'http://yiranblade.cn/csns/resume/submit',
					dataType: 'json',
					contentType: "application/json",
					data: JSON.stringify(data1),
					success: function(data) {
						console.log(data);
						if(data.status == "200") {
							$("#idd").text(data.message);
							$("#s200").show();
							$("#makeSure").bind("click", function() {
								window.location.reload();
							}, false);
						}
						if(data.status == "400") {
							$("#mes").text(data.message);
							$("#s400").show();
							$("#makeSure").bind("click", function() {
								if(flag >= 5) {
									alert("操作次数过多页面将刷新")
									window.location.reload();
								}
								$(".main-bg").show();
								$("#after").hide();
								$("#s400").hide();
								$("#makeSure").hide();
							}, false);
						}
						if(data.status == "500") {
							$("#s500").show();
						}
					},
					error: function(xhr, type) {
						alert('数据加载失败');
					}
				});
				$(".main-bg").hide();
				$("#after").show();
				$("#makeSure").show();
				if(flag != 0) {
					document.getElementById("message").style.top = "19%";
				} else {
					document.getElementById("message").style.top = "19%";

				}
				flag++;
				console.log("flag:" + flag);
				document.getElementById("makeSure").style.width = "100px";
				document.getElementById("makeSure").style.height = "35px";
				document.getElementById("message").style.marginLeft = "-" + document.getElementById("message").clientWidth / 2 + "px";
				document.getElementById("message").style.marginTop = "-" + document.getElementById("message").clientHeight / 2.3 + "px";
			}
		}

	}
}

function emailCheck() {
	var emailStr = $("#Email").val();
	var emailPat = /^[^@](.+)@(.+)\.(.+)[^\.]$/;
	var matchArray = emailStr.match(emailPat);
	if(emailStr != "") {
		if(matchArray == null) {
			alert("电子邮件地址错误");
			setTimeout(function () {
				$("#Email").focus() //焦点恢复
			},300);
			
			return false;
		}
	}
	return true;
}

function telCheck() {
	var telStr = $("#Tel").val();
	var telPat = /^1[3|4|5|7|8][0-9]\d{8}$/;
	var matchArray = telStr.match(telPat);
	if(telStr != "") {
		if(matchArray == null) {
			alert("请检查您的输入手机号!")
				setTimeout(function () {
				$("#Tel").focus() //焦点恢复
			},300)
			return false;
		}
	}

	return true;
}
//去掉输入字符串两边的空格
function trim(s) {
	var count = s.length;
	var st = 0; // start   
	var end = count - 1; // end   

	if(s == "") return s;
	while(st < count) {
		if(s.charAt(st) == " ")
			st++;
		else
			break;
	}
	while(end > st) {
		if(s.charAt(end) == " ")
			end--;
		else
			break;
	}
	return s.substring(st, end + 1);
}

function isEmpty() {

	if(trim($("#Name").val()) == "") {
		alert("用户名不能为空!");
			setTimeout(function () {
				$("#Name").focus() //焦点恢复
			},300)
		return false;
	}
	if(trim($("#Tel").val()) == "") {
		alert("手机号不能为空!");
			setTimeout(function () {
				$("#Tel").focus() //焦点恢复
			},300)
		return false;
	}
	if(trim($("#Email").val()) == "") {
		alert("邮件不能为空!");
			setTimeout(function () {
				$("#Email").focus() //焦点恢复
			},300)
		return false;
	}
	if(trim($("#Class").val()) == "") {
		alert("专业班级不能为空!");
			setTimeout(function () {
				$("#Class").focus() //焦点恢复
			},300)
		return false;
	}
	if(trim($("#Apply").val()) == "") {
		alert("职位+理由不能为空!");
			setTimeout(function () {
				$("#Apply").focus() //焦点恢复
			},300)
		return false;
	}
	return true;
}